package 优先算法.前缀和.寻找数组的中心下标;

/**
 *https://leetcode.cn/problems/find-pivot-index/
 */
public class Main {
    public static void main(String[] args) {
        Solution so = new Solution();
        int[] arr = {};
        int rel = so.pivotIndex(arr);
        System.out.println(rel);
    }
}
class Solution {
    public int pivotIndex(int[] nums) {
        //1.创建一组前缀和数组和后缀和数组（long)防止溢出
        long[] f = new long[nums.length];
        long[] g = new long[nums.length];
        //防止数组越界初始化0和length-1;
        for(int i = 1; i < nums.length; i++){
            //f[i]不包含i的值
            f[i] = f[i-1] + nums[i-1];
        }
        for(int i = nums.length - 2;i >= 0;i--){
            g[i] = g[i+1] + nums[i+1];
        }

        //2.使用数组
        int Max = Integer.MAX_VALUE;
        int rel = Max;
        for(int i = 0;i<nums.length;i++){
            if(f[i] == g[i]){
                rel=Math.min(rel,i);
            }
        }
        return rel==Max?-1:rel;
    }
}